package com.zlq.cn.service.un;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import com.zlq.cn.entity.ZhTibetanDict;
import com.zlq.cn.mapper.un.ZhTibetanDictMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlq.cn.util.StringUtils;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 中文藏文词典 服务实现类
 * </p>
 *
 * @author zhoucl
 * @since 2024-05-04
 */
@Service
public class ZhTibetanDictService extends ServiceImpl<ZhTibetanDictMapper, ZhTibetanDict> {

    @SneakyThrows
    public void pull() {
        String baseUrl = "http://cn.zyw.xizang.gov.cn/bmfw/hzdzgjs/xcy/%s";
        Document document = Jsoup.parse(FileUtil.readString(new File("C:\\Users\\14988\\Desktop\\zuowen.txt"), CharsetUtil.CHARSET_UTF_8));
        for (Element element : document.select(".index-box2-con.gl-gg li a")) {
            String href = element.attr("href");
            href = href.replace(".", "").replace("/", "");
            String pathLetter = StringUtils.defaultIfBlank(href, "A");
            href = String.format(baseUrl, pathLetter + "/index.html");
            document = Jsoup.connect(href).get();

            String page = document.selectFirst(".page").html();
            String[] strings = StringUtils.substringsBetween(page, "createPageHTML(", ",");
            int pageCount = Integer.parseInt(strings[strings.length - 1]);
            List<ZhTibetanDict> zhTibetanDicts = new ArrayList<>();
            for (int i = 0; i < pageCount; i++) {
                if (i > 0) {
                    href = String.format(baseUrl, pathLetter + "/index_" + i + ".html");
                    document = Jsoup.connect(href).get();
                }
                String letter = document.selectFirst(".gl-con-tab .cd-con .title").text();
                for (Element elementLi : document.select(".cd-list li")) {
                    ZhTibetanDict zhTibetanDict = new ZhTibetanDict();
                    zhTibetanDict.setZhCn(elementLi.selectFirst(".lf").text());
                    zhTibetanDict.setLetter(letter);
                    zhTibetanDict.setTibetan(elementLi.selectFirst(".rt").text());
                    zhTibetanDicts.add(zhTibetanDict);
                }
            }
            super.saveBatch(zhTibetanDicts);
        }
    }
}
